/*!
* \file decodethread.h
* \brief 一二维码解析，生成的工作线程
*
*此类是一二维码解析，生成的工作线程
*
*\author 桓乐乐 huanlele@syberos.com
*\version 1.0
*\date 2015/9/7
*/
#ifndef DECODETHREAD_H
#define DECODETHREAD_H
#include <QThread>
#include <QVideoFrame>
#include "multimediautils.h"
/*!
  \class DecodeThread
  \inModule 一二维码解析，生成的工作线程
  \brief 根据传入的进行一二维码解析，并且可以根据文字内容生成一二维码
  \ingroup generateForm
  DecodeThread 一二维码解析，生成的工作线程
*/
class QImage;
class QVideoFrame;

class CVFrame{
public:
    CVFrame():width(0),height(0),bufSize(0),pBuf(NULL){}

    void copyFromQVFrame(QVideoFrame frame)
    {
        pixelFormat = frame.pixelFormat();
        if(pBuf && (width != frame.width() || height != frame.height())){
            free(pBuf);
            pBuf = NULL;
        }
        width = frame.width();
        height = frame.height();
        if(!pBuf){
            bufSize = MultimediaUtils::getInstance()->getBufSize(pixelFormat, width, height);
            pBuf = (uchar*)malloc(bufSize);
        }

        if(pBuf){
            memcpy(pBuf, frame.bits(), bufSize);
        }
    }

    ~CVFrame()
    {
        if(pBuf){
            free(pBuf);
            pBuf = NULL;
        }
    }

    QVideoFrame::PixelFormat pixelFormat;
    int width;
    int height;
    int bufSize;
    uchar *pBuf;
};

class DecodeThread: public QObject
{
    Q_OBJECT
public:
    DecodeThread(QObject *parent = 0);
    ~DecodeThread();
    void decodeBegin(const QVideoFrame &frame);
signals:

    void decodeBeginSignal();
    /**
     * @brief decodeFinished: 解码完成信号
     * @param decodeImagePath: 被解析的一二维码图片的路径
     * @param decodeContent: 解析一二维码图片获取的内容
     * @param success: true，成功； false，失败
     */
    void decodeFinished(const QString &decodeImagePath, const QString &decodeContent, const bool &success);
public slots:
    /**
     * @brief setFrame: 设置要解析的图片的帧
     * @param frame: 帧
     */
    //void beginDecode(QVideoFrame frame);

    void decodeFrame();
private:
    volatile bool m_nDecode; ///< true, 解析一二维码图片；false，生成二维码图片
    QThread workThread;
    CVFrame mVFrame;
};

#endif // DECODETHREAD_H
